package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Hyfl;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class HyflForm extends BasePage implements Serializable {
    private GenericManager<Hyfl, Long> hyflManager;
    private Hyfl hyfl = new Hyfl();
    private Long id;

    public void setHyflManager(GenericManager<Hyfl, Long> hyflManager) {
        this.hyflManager = hyflManager;
    }

    public Hyfl getHyfl() {
        return hyfl;
    }

    public void setHyfl(Hyfl hyfl) {
        this.hyfl = hyfl;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        hyflManager.remove(hyfl.getId());
        addMessage("hyfl.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            hyfl = hyflManager.get(id);
        } else {
            hyfl = new Hyfl();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (hyfl.getId() == null || hyfl.getId() == 0);
        
        hyfl.setUsername(getRequest().getRemoteUser());
        hyfl.setOptdate(new Date());
        hyfl.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
     
        hyflManager.save(hyfl);

        String key = (isNew) ? "hyfl.added" : "hyfl.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
           	
     	   this.id=this.hyfl.getId();
              return edit();


        }
    }
} 